use "ESS.dta", clear

drop if cntry == "GB" | cntry == "IL"

encode country, gen(Country_new)

tempname results
postfile `results' str20 country_name reform se p_value bandwidth N mean using temp_results.dta, replace

 
local i = 0
foreach Country_new of numlist 1/22 {
    local i = `i' + 1
    local label: label Country_new `Country_new'
    display "Processing Country_new = `label'"
    
    rdrobust University running if Country_new == `Country_new' , kernel(uniform) masspoints(off)
    
    
    local tau_cl = round(e(tau_cl), .001)
    local se_tau_cl = round(e(se_tau_cl), .001)
    local pv_cl = round(e(pv_cl), .001)
    local bandwidth = floor(e(h_l) + e(h_r))
    local N_h_l = e(N_h_l) + e(N_h_r)
    
    sum University if running >= -floor(e(h_l)) & running <= floor(e(h_l)) & Country_new == `Country_new'
    local mean = round(r(mean), 0.001)
    
     
    post `results' ("`label'") (`tau_cl') (`se_tau_cl') (`pv_cl') (`bandwidth') (`N_h_l') (`mean')
}

 postclose `results'

 
use temp_results.dta, clear

 matrix ALL_BALANCE = J(_N, 6, .)
forvalues i = 1/`=_N' {
    matrix ALL_BALANCE[`i',1] = reform[`i']
    matrix ALL_BALANCE[`i',2] = se[`i']
    matrix ALL_BALANCE[`i',3] = p_value[`i']
    matrix ALL_BALANCE[`i',4] = bandwidth[`i']
    matrix ALL_BALANCE[`i',5] = N[`i']
    matrix ALL_BALANCE[`i',6] = mean[`i']
}

 matrix rownames ALL_BALANCE = "Albania" "Austria" "Belgium" "Croatia" "Czech Republic" ///
                              "Denmark" "Estonia" "Finland" "France" "Germany" "Hungary" ///
                              "Italy" "Latvia" "Lithuania" "Netherlands" "Norway" "Poland" ///
                              "Portugal" "Slovak Republic" "Slovenia" "Spain" "Sweden"

 matrix colnames ALL_BALANCE = "Reform" "SE" "p-value" "Bandwidth" "N." "Mean"

 esttab matrix(ALL_BALANCE) using "T3.tex", ///
     cells("Reform SE  p-value Bandwidth  N.  Mean") ///
    booktabs ///
    prehead("\begin{table}[htbp]\centering \footnotesize  \caption{\scshape Additional: Bologna process and attainment, by country \label{RDD}}     \begin{threeparttable}  \begin{tabular}{l  cccccc}   \toprule" ) ///
    postfoot("\toprule  \end{tabular}  \begin{tablenotes} \setlength\labelsep{0pt}  \item \scriptsize {\textit{Notes.} We run a regression discontinuity design for each country using rdrobust  (\cite{calonico2017rdrobust}). We fit the model using a first-order polynomial, quadratic polynomial for bias correction, triangular kernel, and  MSE-optimal bandwidth selector for the RD treatment effect estimator. We do not restrict the cohorts as in the TWFE specification, allowing the bandwidth to be selected from a longer period.}  \end{tablenotes}   \end{threeparttable}     \end{table}") ///
    replace label
